﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using UnitControls;
using rFDevLib;

namespace TGMTireBuilder {

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window {
        private BasicSpecs newBasics;
        private TGMTire tire;
        private TGMGraphic tireGfx;
        private TGMDimensions specs;
        private MaterialsLib matLib;

        public MainWindow() {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

            InitializeComponent();

            tire = new TGMTire();
            tireGfx = new TGMGraphic();
            specs = new TGMDimensions();
            UpdateGfx();
        }

        private void BtnQuickFill_Click( object sender, RoutedEventArgs e ) {
            newBasics = new BasicSpecs();
            newBasics.Closing += NewBasics_Closing;

            newBasics.Accepted = false;
            newBasics.ShowDialog();
        }

        void NewBasics_Closing( object sender, System.ComponentModel.CancelEventArgs e ) {
            if ( !newBasics.Accepted ) return;

            inTreadWidth.Value = newBasics.GetTreadWidth();
            inSidewallHeight.Value = newBasics.GetSidewallHeight();
            inRimRadius.Value = newBasics.GetRimRadius();
            inRimWidth.Value = Math.Round( inTreadWidth.Value * 2.0 * 39.37007874015748 ) / 2.0 / 39.37007874015748;

            UpdateGfx();
        }

        private void UpdateGfx() {
            specs.treadWidth = inTreadWidth.Value;
            specs.sidewallHeight = inSidewallHeight.Value;
            specs.rimRadius = inRimRadius.Value;
            specs.rimWidth = inRimWidth.Value;
            specs.sidewallBulge = inSidewallBulge.Value;
            specs.ringBulge = inRingBulge.Value;
            specs.latRes = Math.Max(inLatResolution.Value, 4);
            specs.radRes = inRadialResolution.Value;

            specs.cornerLength = inCornerLength.Value;
            specs.cornerRounding = inCorner.Value;

            tire.m_refPres = new List<double>();
            for ( int i = 0; i < RefPressureList.GetCount(); i++ ) {
                tire.m_refPres.Add( RefPressureList.GetValue( i ) );
            }

            tire.m_refSpeed = new List<double>();
            double circumference = specs.GetCircumference();
            for ( int i = 0; i < RefSpeedList.GetCount(); i++ ) {
                tire.m_refSpeed.Add( Math.Pow((RefSpeedList.GetValue( i ) / circumference) * 2.0 * Math.PI, 2.0) );
            }

            tire.BuildFromSpecs( specs );
            tireGfx.NewGraphic(specs);

            tire.DrawGraphic( tireGfx );

            imDiagram.Source = tireGfx.GetBmpSrc();
        }

        private void OnAnyChange( object sender, EventArgs e ) {
            UpdateGfx();
        }

        private void BtnExport_Click( object sender, RoutedEventArgs e ) {
            string defaultName = Math.Round(specs.treadWidth*1000.0).ToString() + "-" +
                Math.Round( (specs.sidewallHeight * 100.0) / specs.treadWidth ).ToString() + "-" +
                Math.Round( specs.rimRadius * 2.0 * 39.37007874015748 ) + "x" +
                Math.Round( specs.rimWidth * 39.37007874015748 );

            Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
            dlg.FileName = defaultName;
            dlg.DefaultExt = ".tgm";
            dlg.Filter = "rFactor 2 tires (.tgm)|*.tgm";

            Nullable<bool> result = dlg.ShowDialog();

            if ( result != true ) return;

            System.IO.StreamWriter w = new System.IO.StreamWriter( dlg.FileName );
            tire.WriteToStream( w );
            w.Close();
        }
    }
}
